﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.GoodsModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmGoodsPurchase : Form
    {
        public FrmGoodsPurchase()
        {
            InitializeComponent();
        }

        GoodsManager goodsManager = new GoodsManager();
        int inventory = 0;

        private void FrmGoodsPurchase_Load(object sender, EventArgs e)
        {
            try
            {
                List<Goods> list = new List<Goods>();
                list = goodsManager.GetGoodss();
                cmbSelectGoodsID.Items.Clear();
                cmbSelectGoodsID.Items.Add("新商品");
                foreach (Goods g in list)
                {
                    cmbSelectGoodsID.Items.Add(g.GoodsID);
                }
                cmbSelectGoodsID.Focus();
                btnPurchase.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmbSelectGoodsID_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbSelectGoodsID.SelectedIndex == 0)
                {
                    txtGoodsID.ReadOnly = false;
                    txtName.ReadOnly = false;
                    txtInPrice.ReadOnly = false;
                    txtOutPrice.ReadOnly = false;
                    txtGoodsID.Text = "";
                    txtName.Text = "";
                    txtInPrice.Text = "";
                    txtOutPrice.Text = "";
                    txtGoodsID.Focus();
                    btnPurchase.Enabled = true;
                }
                else
                {
                    string goodsID = cmbSelectGoodsID.SelectedItem.ToString();
                    Goods goods = goodsManager.GetGoods(goodsID);
                    txtGoodsID.Text = goods.GoodsID;
                    txtName.Text = goods.Name;
                    txtInPrice.Text = goods.InPrice.ToString();
                    txtOutPrice.Text = goods.OutPrice.ToString();
                    inventory = goods.Inventory;
                    txtGoodsID.ReadOnly = true;
                    txtName.ReadOnly = true;
                    txtInPrice.ReadOnly = true;
                    txtOutPrice.ReadOnly = true;
                    txtInventory.Focus();
                    btnPurchase.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnPurchase_Click(object sender, EventArgs e)
        {
            try
            {
                if (cmbSelectGoodsID.SelectedIndex == 0)
                {
                    if (txtGoodsID.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入商品号", "提示");
                        txtGoodsID.Focus();
                        return;
                    }
                    if (txtName.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入商品名", "提示");
                        txtName.Focus();
                        return;
                    }
                    if (txtInPrice.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入进货价", "提示");
                        txtInPrice.Focus();
                        return;
                    }
                    if (txtOutPrice.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入售货价", "提示");
                        txtOutPrice.Focus();
                        return;
                    }
                    if (txtInventory.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入进货量", "提示");
                        txtInventory.Focus();
                        return;
                    }
                    Goods goods = new Goods();
                    goods.GoodsID = txtGoodsID.Text.Trim();
                    goods.Name = txtName.Text.Trim();
                    goods.InPrice = decimal.Parse(txtInPrice.Text.Trim());
                    goods.OutPrice = decimal.Parse(txtOutPrice.Text.Trim());
                    goods.Inventory = int.Parse(txtInventory.Text.Trim());
                    if (goodsManager.AddGoods(goods))
                    {
                        MessageBox.Show("新商品进货成功", "提示");
                        txtGoodsID.Text = "";
                        txtName.Text = "";
                        txtInPrice.Text = "";
                        txtOutPrice.Text = "";
                        txtInventory.Text = "";
                        txtGoodsID.ReadOnly = true;
                        txtName.ReadOnly = true;
                        txtInPrice.ReadOnly = true;
                        txtOutPrice.ReadOnly = true;
                        FrmGoodsPurchase_Load(sender, e);
                    }
                }
                else
                {
                    if (txtInventory.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入进货量", "提示");
                        txtInventory.Focus();
                        return;
                    }
                    Goods goods = new Goods();
                    goods.GoodsID = txtGoodsID.Text.Trim();
                    goods.Name = txtName.Text.Trim();
                    goods.InPrice = decimal.Parse(txtInPrice.Text.Trim());
                    goods.OutPrice = decimal.Parse(txtOutPrice.Text.Trim());
                    goods.Inventory = inventory + int.Parse(txtInventory.Text.Trim());
                    if (goodsManager.ChangeGoods(goods))
                    {
                        MessageBox.Show("库存修改成功", "提示");
                        txtGoodsID.Text = "";
                        txtName.Text = "";
                        txtInPrice.Text = "";
                        txtOutPrice.Text = "";
                        txtInventory.Text = "";
                        FrmGoodsPurchase_Load(sender, e);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtInventory_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnPurchase_Click(sender, e);
            }
        }
    }
}
